<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

use Think\Controller;

class OperatorController extends Controller {

    public function add() {
        if (!empty($_POST)) {

            $func = new \Admin\Func\Func901010101(I('OperatorID'), I('OperatorName'), I('LoginPasswd'));

            if ($func->execute() === false) {
                $this->error("新增失败:" . $func->getErrMsg());
            } else {
                // $this->display();
                $this->success('新增成功！');
            }
            //  $this->display();
        } else {
            $this->display();
        }
    }

    public function del($OperatorID) {
        $func = new \Admin\Func\Func901010103($OperatorID);
        if ($func->execute() === false) {
            $this->error('删除失败' . $func->getErrMsg());
        } else {
            $this->redirect('Operator/lst');
        }
    }

    public function lst() {

        $func = new \Admin\Func\Func901010105(I('OperatorName'));
        if ($func->execute() === false) {
            $this->error('查询失败:' . $func->getErrMsg());
        } else {

            $this->assign('lst', $func->getRst());
        }
        $this->display();
    }

    public function edit($OperatorID) {

        if (!empty($_POST)) {
            $func = new \Admin\Func\Func901010102(I('OperatorID'), I('OperatorName'));
            if ($func->execute() === false) {
                $this->error("编辑失败:" . $func->getErrMsg());
            } else {

                $this->success('编辑成功！');
            }
        } else {

            $func = new \Admin\Func\Func901010104($OperatorID);

            if ($func->execute() === false) {
                $this->assign('error', '失败:' . $func->getErrMsg());
            } else {
                $this->assign('post', $func->getRst()[0]);
                $this->display();
            }
        }
    }

    public function login2() {
        if (!empty($_POST)) {
            $func = new \Admin\Func\Func901010104(I('OperatorID'));
            $func->execute();
            show_bug($func->getRst());
            show_bug(I('LoginPasswd'));
            // $this->display();
        }
        $this->display();
    }

    public function login() {
        if (!empty($_POST)) {
            $verify = new \Think\Verify();
            if (!$verify->check($_POST['captcha'])) {
                $this->error("验证码错误");
            } else {

                //判断用户名和密码，在model模型里边制作一个专门方法进行验证
                // $user = new \Admin\Model\OperatorModel();

                $func = new \Admin\Func\Func901010104(I('OperatorID'));

                if ($func->execute() === false) {
                    $this->error('系统错误，请联系管理员:' . $func->getErrMsg());
                } else {
                    if (count($func->getRst()) === 0) {
                        $this->error('用户ID不存在');
                    } else {

                        // $this->error($rst[0]['loginpasswd']);
                        if ($func->getRst()[0]['loginpasswd'] != I('LoginPasswd')) {

                            $this->error('密码错误');
                        } else {
                            session('OperID', $func->getRst()[0]['operatorid']);
                            session('OperName', $func->getRst()[0]['operatorname']);
                            $this->success("验证成功，请使用!");
                        }
                    }
                }
            }
        } else {

            $this->display();
        }
    }

    public function logout() {
        echo "Logouting";
    }

    //制作专门方法实现验证码生成
    function verifyImg() {
        //以下类Verify在之前并没有include引入
        //走自动加载Think.class.php  autoload()
        $config = array(
            'imageH' => 25, // 验证码图片高度
            'imageW' => 105,
            'fontSize' => 15,
            'useNoise' => FALSE, // 是否添加杂点	
            'useCurve' => FALSE, // 是否画混淆曲线
            //  'fontttf' => '4.ttf', // 验证码字体，不设置随机获取
            'length' => 4, // 验证码位数
        );
        $verify = new \Think\Verify($config);
        $verify->entry();
    }

}
